package com.socool.sinofaith.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.adapter.CompleteTaskAdapter;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.CompleteTaskMessage;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.LoadingDialog;

/**
 * 舍弃
 *
 */
public class QuestionStempActivity extends BaseActivity  implements
CenterViewFactory, SpinnerViewFactory {

	ActionBarHelperBase mActionBarHelperBase;
	private PullToRefreshListView mPullRefreshListView;
	private ListView listView;
	private CompleteTaskAdapter completeTaskAdapter;
	private Button btnSearch;
	private EditText taskname;
	private int currentIndex = 1;
	private String TemplateName;

	private LoadingDialog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SocoolApplication.addActivity(this);
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);
		setContentView(R.layout.fragment_complete);
		mLoadingDialog = new LoadingDialog(this);
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.listview);
		btnSearch = (Button) findViewById(R.id.btnSearch);
		taskname = (EditText) findViewById(R.id.taskname);

	}


	@Override
	public View createSpinnerView() {
		// TODO Auto-generated method stub
		return new ImageView(this);
	}

	@Override
	public View createView() {
		TextView tv = new TextView(this);
		tv.setText(TemplateName);
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		tv.setSingleLine(true);
		tv.setMaxEms(10);
		tv.setEllipsize(TruncateAt.END);
		return tv;
	}
	void init() {
		completeTaskAdapter = new CompleteTaskAdapter(this);
		listView = mPullRefreshListView.getRefreshableView();
		listView.setAdapter(completeTaskAdapter);

		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						String label = DateUtils.formatDateTime(
								QuestionStempActivity.this,
								System.currentTimeMillis(),
								DateUtils.FORMAT_SHOW_TIME
										| DateUtils.FORMAT_SHOW_DATE
										| DateUtils.FORMAT_ABBREV_ALL);

						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);

						// 清空数据重新请求
						completeTaskAdapter.getMdatas().clear();
						currentIndex = 1;
						getData();
					}
				});
		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
					@Override
					public void onLastItemVisible() {
						// lastLoadingView.setVisibility(View.VISIBLE);
						currentIndex++;
						getData();
					}
				});

		btnSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// 清空数据重新请求
				completeTaskAdapter.getMdatas().clear();
				currentIndex = 1;
				getData();
			}
		});

		// 清空数据重新请求
		completeTaskAdapter.getMdatas().clear();
		currentIndex = 1;
		getData();
	}

	@Override
	public void onResume() {
		// 清空数据重新请求
		init();
		super.onResume();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getData() {
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.completeTask_url);
		params.put("did", SinofaithUtil.did);
		params.put("mobile", getSPString());
		// params.put("mobile", "13400000000");
		params.put("pageNum", currentIndex + "");
		params.put("pageSize", SinofaithUtil.pageSize + "");
		params.put("taskname", taskname.getText().toString());

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal
				.getTime());
		Log.v("qqqq", yesterday);
		params.put("date", yesterday);
		fh.get(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mLoadingDialog.show();
			}

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				mLoadingDialog.dismiss();
				super.onSuccess(t);
				// Toast.makeText(YesterdayFinishFragment.this.getActivity(),
				// t+"", Toast.LENGTH_LONG).show();
				try {
					CompleteTaskMessage completeTaskMessage = HttpJsonAdapter
							.getInstance().get(t.toString(),
									CompleteTaskMessage.class);
					if ("0".equals(completeTaskMessage.getResult())) {
						completeTaskAdapter.addDatas(completeTaskMessage
								.getCompleteTaskList());
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mPullRefreshListView.onRefreshComplete();
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				mLoadingDialog.dismiss();
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(QuestionStempActivity.this, strMsg,
						Toast.LENGTH_LONG).show();
				mPullRefreshListView.onRefreshComplete();
			}

		});
	}
	private String getSPString(){
		SharedPreferences sp=this.getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}

}
